﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GMap.NET.WindowsForms
{
   public class GMapCircle
   {
      public GMapCircle(PointLatLng center, double radius)
      {
         Center = center;
         Radius = radius;
      }

      GMapOverlay overlay;
      public GMapOverlay Overlay
      {
         get
         {
            return overlay;
         }
         internal set
         {
            overlay = value;
         }
      }

      private bool visible = true;

      /// <summary>
      /// is polygon visible
      /// </summary>
      public bool IsVisible
      {
         get
         {
            return visible;
         }
         set
         {
            if (value != visible)
            {
               visible = value;

               if (Overlay != null && Overlay.Control != null)
               {
                  if (visible)
                  {
                     Overlay.Control.UpdateCircleLocalPosition(this);
                  }
                  else
                  {
                     if (Overlay.Control.IsMouseOverPolygon)
                     {
                        Overlay.Control.IsMouseOverPolygon = false;
#if !PocketPC
                        Overlay.Control.RestoreCursorOnLeave();
#endif
                     }
                  }

                  {
                     if (!Overlay.Control.HoldInvalidation)
                     {
                        Overlay.Control.Invalidate();
                     }
                  }
               }
            }
         }
      }

      /// <summary>
      /// can receive input
      /// </summary>
      public bool IsHitTestVisible = false;

      private bool isMouseOver = false;

      /// <summary>
      /// is mouse over
      /// </summary>
      public bool IsMouseOver
      {
         get
         {
            return isMouseOver;
         }
         internal set
         {
            isMouseOver = value;
         }
      }

      public PointLatLng Center
      {
         get;
         set;
      }

      public double Radius
      {
         get;
         set;
      }

      public GPoint LocalCenter
      {
         get;
         set;
      }

      /// <summary>
      /// Indicates whether the specified point is contained within this System.Drawing.Drawing2D.GraphicsPath
      /// </summary>
      /// <param name="x"></param>
      /// <param name="y"></param>
      /// <returns></returns>
      internal bool IsInsideLocal(int x, int y)
      {
         if (rectangle != null)
         {
            var path = new GraphicsPath();
            path.AddEllipse(rectangle);
            return path.IsVisible(x, y);
         }
         return false;
      }

      RectangleF rectangle;

      internal void UpdateRectangle()
      {
         var scale = Math.Pow(2, (18 - Overlay.Control.Zoom)) * 0.5971642833709717;
         var radius = this.Radius / scale;
         rectangle = new RectangleF(this.LocalCenter.X - (float)radius, this.LocalCenter.Y - (float)radius, 2 * (float)radius, 2 * (float)radius);
      }

      /// <summary>
      /// 绘制
      /// </summary>
      /// <param name="g"></param>
      public virtual void OnRender(Graphics g)
      {
         if (IsVisible)
         {
            
            g.FillEllipse(this.Fill, rectangle);
            g.DrawEllipse(this.Stroke, rectangle);
         }
      }


      public static readonly Pen DefaultStroke = new Pen(Color.FromArgb(155, Color.MidnightBlue));
      //边线
      public Pen Stroke = DefaultStroke;

      public static readonly Brush DefaultFill = new SolidBrush(Color.FromArgb(155, Color.AliceBlue));
      //填充
      public Brush Fill = DefaultFill;
   }

   public delegate void CircleClick(GMapCircle item, MouseEventArgs e);
   public delegate void CircleEnter(GMapCircle item);
   public delegate void CircleLeave(GMapCircle item);
   public delegate void CircleDoubleClick(GMapCircle item, MouseEventArgs e);
}
